# Author    :Dun_Hz
# Time      :2025-04-07 15:31

n=int(input())
st=set()
infos= {}
for i in range(n):
    timeStamp, client, factor, num = input().split(",")
    info=f'{timeStamp},{client},{factor}'
    if info in st:
        continue
    st.add(info)
    infos[client]=infos.get(client, {})
    if 0<=int(num)<=100:
        infos[client][factor]=infos[client].get(factor, 0)+int(num)
m=int(input())
factors={}
for _ in range(m):
    factor, num = input().split(',')
    factors[factor]=int(num)

keys=list(infos.keys())
keys.sort()
for client in keys:
    total=0
    for factor in infos[client]:
        if factor in factors:
            total+=infos[client][factor]*factors[factor]
    print(f"{client},{total}")